﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class leks : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] != null && Session["usertype"] != null && Session["stuproType"] != null)
        {
            Response.Redirect("stuks.aspx");
            return;
        }
        if (!IsPostBack)
        {
            //时钟
            MagicAjax.AjaxCallHelper.SetAjaxCallTimerInterval(1000); 
        }
        Lb_NowTime.Text = "现在的时间是：" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DateTime NowTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

        DateTime openTime = DateTime.Parse(SQL.GetData("select OpenDate from SysOperter", "OpenDate"));

        DateTime colseTime = DateTime.Parse(SQL.GetData("select ColseDate from SysOperter", "ColseDate"));

        if ((NowTime < openTime || NowTime > colseTime) && RadioButtonList1.SelectedItem.Text.Equals("学生"))
        {
            Anthem.Manager.AddScriptForClientSideEval("alert('考试已经结束,无法登陆！')");
            return;
        }



        if (Session["username"] != null || Session["usertype"] != null)
        {
            Anthem.Manager.AddScriptForClientSideEval("alert('已经登录！')");
            return;
        }
        if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))
        {
            Anthem.Manager.AddScriptForClientSideEval("alert('未填写用户名或密码！')");
            return;
        }
        //组织whereSQL语句
        StringBuilder mSbuilder = new StringBuilder();
        mSbuilder.Append(SqlFormat.FormatWhere("wtusername", publicConn.Quote(TextBox1.Text.Trim()), true));
        mSbuilder.Append(SqlFormat.FormatWhere("wtpassword", publicConn.Quote(TextBox2.Text.Trim()), true));
        mSbuilder.Append(SqlFormat.FormatWhere("wtusertype", RadioButtonList1.SelectedItem.Text.ToString(), true, true));

        if (SQL.ExecScalar("select count(*) from wtuser where " + mSbuilder.ToString()) > 0)
        {
            Session["username"] = TextBox1.Text.Trim();
            Session["usertype"] = RadioButtonList1.SelectedItem.Text.ToString();
            if (RadioButtonList1.SelectedItem.Text.Equals("学生"))
            {
                string wtprotype = SQL.GetData("select wtprotype from wtuser where " + mSbuilder.ToString(), "wtprotype");
                if (string.IsNullOrEmpty(wtprotype))
                {
                    // 防止数据库错误
                    Anthem.Manager.AddScriptForClientSideEval("alert('登陆错误！')");
                    CookieOper.CleanSession("username");
                    CookieOper.CleanSession("usertype");
                    return;
                }
                Session["stuproType"] = wtprotype;
                
                Response.Redirect("stuks.aspx");
            }
            else if (RadioButtonList1.SelectedItem.Text.Equals("管理员"))
            {
                Response.Redirect("system/Manage/index.htm");
            }
            else
            {
                //预留教师登陆
            }
        }
        else
        {
            Anthem.Manager.AddScriptForClientSideEval("alert('输入信息有错误，请确认！')");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        RadioButtonList1.SelectedIndex = 0;
        RadioButtonList1.UpdateAfterCallBack = true;
        TextBox1.UpdateAfterCallBack = true;
        TextBox2.UpdateAfterCallBack = true;
    }
}
